//
//  TTHomeViewController.m
//  TaoTuo
//
//  Created by admin on 2019/8/15.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTHomeViewController.h"
#import "TTHomeHeaderView.h"
//#import <JXPagingView/JXPagerView.h>
//#import <JXCategoryView.h>
//#import "JXCategoryTitleAttributeView.h"
//#import "TTHomeListView.h"
#import "TTNewHomeListView.h"
#import "TTHomeModel.h"
#import "TTCategoryViewController.h"
#import "TTUpDownButton.h"

#import "TTGoodsListViewController.h"
#import "TTMessageViewController.h"
#import "TTUserModel.h"

#import "TTMarketModel.h"
#import "TTMarketViewController.h"

#import "TTADView.h"

#import "TTRegisterViewController.h"

#import "LLAlertView.h"
#import <TYShowAlertView.h>
#import "TTUpdateModel.h"
#import <UIView+TYAlertView.h>
#import "TTHongBaoView.h"



@interface TTHomeViewController ()

@property (nonatomic, strong) TTHomeHeaderView *headerView;
@property (nonatomic, strong) NSMutableArray *allGoldArray; // 金价
@property (nonatomic, assign) NSInteger currentIndex;   // 当前在第几个

@property (nonatomic, strong) TTHomeModel *homeModel;
@property (nonatomic, strong) TTHongBaoView *hongbaoView;   ///红包

@property (nonatomic, strong) UIButton *toTopBtn;   // 点击滚到顶部
@property (nonatomic, assign) BOOL hongbaoAppear;   ///红包页面已经出现过了
@property (nonatomic, assign) BOOL adDismiss;   ///广告页面消失了
@property (nonatomic, strong) TTNewHomeListView *listView;

@end

@implementation TTHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.fd_prefersNavigationBarHidden = YES;
    // 添加广告页面
    [self addADPage];

    [self setupSubviews];
    // 缓存数据展示
    [self showCacheData];

    [self getTopNetworkDataWithLoadingView:self.view];

    // 购物车条码 数量
    [TTCommonNetData getShopCartCodeInfoSuccess:^(NSArray * _Nonnull cartCodeArray, int shopCount) {} failure:^{}];

    // 行情中心开关变化
    [self handleMarketAndHeaderVIewHeight];

    //上线通知
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self userOnlineNotify];
    });
    
    
}

// 显示缓存数据
- (void)showCacheData
{
    // 先取缓存数据
    TTUserModel *userModel = [LLPasswordManagerUtilies getDatasWithKey:saveUserVipKey];
    TTHomeModel *model = [LLPasswordManagerUtilies getDatasWithKey:saveHomeTopDataKey];
    NSArray *marketArray = [LLPasswordManagerUtilies getDatasWithKey:saveGoldDatasKey];

    self.homeModel = model;
    [self handleDatasWithHomeModel:model marketArray:marketArray description:@" " userModel:userModel];
}

- (void)searchBtnClick
{
    TTMessageViewController *messageVC = [[TTMessageViewController alloc] init];
    [self.navigationController pushViewController:messageVC animated:YES];
}

- (void)setupSubviews
{
    [self addTopSearchView];
    
    _headerView = [TTHomeHeaderView headerViewWithBannerArray:@[]];
    @weakify(self)
    _headerView.clickGoodsBlock = ^(NSString * _Nonnull title) {
        @strongify(self)
        
        if ([title containsString:@"全部款式"]) {
            TTCategoryViewController *categoryVC = [[TTCategoryViewController alloc] init];
            [self.navigationController pushViewController:categoryVC animated:YES];
        } else if ([title isEqualToString:@"裸石"]) {
            
            TTMessageViewController *messageVC = [[TTMessageViewController alloc] init];
            messageVC.defaultSelectIndex = 1;
            [self.navigationController pushViewController:messageVC animated:YES];
            
        } else {
            NSArray *array = [TTGoodsListViewController getAllCategoryTitles];
            NSInteger index = [array indexOfObject:title];
            TTGoodsListViewController *goodsVC = [[TTGoodsListViewController alloc] initWithSearchName:title paramsDic:nil selectIndex:index];
            [self.navigationController pushViewController:goodsVC animated:YES];
        }
    };
    
    [self.listView.collectionView addSubview:_headerView];
    
    self.listView.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self)
        [self getTopNetworkDataWithLoadingView:nil];
        [self.listView refreshData];
    }];
    
}

// 添加广告页面
- (void)addADPage
{
    @weakify(self)
    [TTADView adViewAndDismissBlock:^{
        @strongify(self)
        self.adDismiss = YES;
        // 更新提示
        [self updateAppHint];
        //红包
        [self showHongbaoPage];
        
        //公告
        [self showNotifyInfo];
        
    }];
}

///红包页面
- (void)showHongbaoPage
{
    TTLoginSingle *single = [TTLoginSingle sharedSingle];
    @synchronized (self) {
        if (single.userModel.IsDisplayNewUserEnjoyment && !self.hongbaoAppear && self.adDismiss) {
            [self.hongbaoView setHeaderImageWithUrl:single.loginModel.SalesmanPicture kefuName:single.loginModel.SalesmanAccountName];
            self.hongbaoView.transform = CGAffineTransformIdentity;
            [self.hongbaoView showInWindowWithBackgoundTapDismissEnable:YES];
            self.hongbaoAppear = YES;
        }
    }
}

// 行情中心开关变化
- (void)handleMarketAndHeaderVIewHeight
{
    @weakify(self)
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"goldMarketListRefreshNotify" object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        @strongify(self)
        self.allGoldArray= x.object;
        NSMutableArray *array = [NSMutableArray array];
        for (TTMarketModel *model in self.allGoldArray) {
            if (model.isOpen) {
                [array addObject:model];
            }
        }
        [self.headerView setGoldArrayWithArray:array];
        [self.headerView layoutIfNeeded];
        self.listView.sectionInsets = UIEdgeInsetsMake(self.headerView.height + 10, 10, 10, 10);

        // 保存要显示的
        [LLPasswordManagerUtilies saveDatasWithKey:saveGoldDatasKey object:array];
        
    }];
    
    // 登录或退出成功
    [[TTLoginSingle sharedSingle].loginSubject subscribeNext:^(NSNumber *  _Nullable x) {
        @strongify(self)
        NSMutableArray *array = [self handleGoldData];
        
        [self.headerView setGoldArrayWithArray:array];
        [self.headerView layoutIfNeeded];
        self.listView.sectionInsets = UIEdgeInsetsMake(self.headerView.height + 10, 10, 10, 10);
        
        if (x.intValue == 1) {
            // 登录了
            [self getTopNetworkDataWithLoadingView:nil];
        } else {
            // 退出登录
            [LLPasswordManagerUtilies saveDatasWithKey:saveHomeTopDataKey object:self.homeModel];
            self.headerView.has = NO;
            [self.headerView layoutIfNeeded];
            self.listView.sectionInsets = UIEdgeInsetsMake(self.headerView.height + 10, 10, 10, 10);

        }
        
    }];
}

// 获取批发商身份信息(会员状态、账号状态、是否新老用户)
- (COPromise *)getVipLevel
{
    COPromise *promise = [COPromise promise];
    [TTCommonNetData getVipLevelSuccess:^(TTUserModel * _Nonnull userModel) {
        [promise fulfill:userModel];
    } failure:^{
        [promise fulfill:nil];
    }];
    
    return promise;
}


// 上部数据
- (COPromise *)getHomeInitData
{
    COPromise *promise = [COPromise promise];
    
    [[LLNetworkManagerTool sharedManager] requestMethod:GET urlString:homeInitAsync_api parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
//        self.noDataView.hidden = YES;
        TTHomeModel *m = [TTHomeModel modelWithJSON:responseObject];
        
        [promise fulfill:m];
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
//        self.noDataView.hidden = NO;
        [promise fulfill:nil];
        
    } showLoadingInView:nil];
    
    return promise;
}

// 处理数据
- (void)getTopNetworkDataWithLoadingView:(UIView *)view
{
    co_launch(^{
        // 用户信息
        TTUserModel *userModel = await([self getVipLevel]);
        [self showHongbaoPage];
        
        // 上部数据
        TTHomeModel *model = await([self getHomeInitData]);
        self.homeModel = model;
        [self showNotifyInfo];
        // 金价行情
        NSString *description = nil;
        NSArray *marketArray = nil;
        co_unpack(&description, &marketArray) = await([self getMarketData]);
        
        // 保存数据，做缓存用
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [LLPasswordManagerUtilies saveDatasWithKey:saveUserVipKey object:userModel];
            [LLPasswordManagerUtilies saveDatasWithKey:saveHomeTopDataKey object:model];
            [LLPasswordManagerUtilies saveDatasWithKey:saveGoldDatasKey object:marketArray];
        });
        
        [self handleDatasWithHomeModel:model marketArray:marketArray description:description userModel:userModel];
        
        [self.listView.collectionView.mj_header endRefreshing];
    });
    
}
/// 保存公告id
- (void)saveNotifyIDWithDic:(NSMutableDictionary *)dic
{
    [dic setObject:@(YES) forKey:self.homeModel.Notice.NoticeId];
    [USER_DEFAULT setObject:dic forKey:notifyID_key];
    [USER_DEFAULT synchronize];
}

///公告弹窗
- (void)showNotifyInfo
{
    if (![TTLoginSingle sharedSingle].isLogin) {
        return;
    }
    NSMutableDictionary *dic = [[USER_DEFAULT valueForKey:notifyID_key] mutableCopy];
    if (dic == nil) {
        dic = [NSMutableDictionary dictionary];
    }
    //广告消失之后才显示
    if ([dic[self.homeModel.Notice.NoticeId] boolValue] || !self.adDismiss || self.homeModel.Notice.Summary.length == 0) {
        return;
    }
    
    LLSystemAlertView *alertView = [LLSystemAlertView alertViewWithTitle:self.homeModel.Notice.Title message:self.homeModel.Notice.Summary cancelTitle:@"查看详情" cancelBlock:^{
        [self saveNotifyIDWithDic:dic];
        [LLRouteTool skipViewControllerWIthSkipType:@"19101" skipTarget:self.homeModel.Notice.NoticeId];
    } confirmTitle:@"好的" confirmBlock:^BOOL{
        [self saveNotifyIDWithDic:dic];
        return YES;
    }];
    alertView.titleColor = [UIColor whiteColor];
    alertView.titleBackgroundColor = kMainColor;
    alertView.cancleTitleColor = UIColorHex(666666);
    alertView.confirmTitleColor = kMainColor;
    
//    [alertView showInView:nil animationDuration:0];
    [alertView showInWindowWithBackgoundTapDismissEnable:YES];
    
}

- (void)handleDatasWithHomeModel:(TTHomeModel *)model marketArray:(NSArray *)marketArray description:(NSString *)description userModel:(TTUserModel *)userModel
{
    if (model != nil) {
        @weakify(self)
        [self.headerView setHomeModel:model marketArray:marketArray upDateTime:description clickMoreButtonBlock:^{
            @strongify(self)
            TTMarketViewController *marketVC = [[TTMarketViewController alloc] init];
            marketVC.dataArray = self.allGoldArray;
            [self.navigationController pushViewController:marketVC animated:YES];
        }];
        
    }
    
    
    [self.headerView layoutIfNeeded];
    self.listView.sectionInsets = UIEdgeInsetsMake(self.headerView.height + 10, 10, 10, 10);
    
    if (userModel != nil && userModel.State == 0) {
        // 账号禁用，清空账号信息，去登录
        [LLPasswordManagerUtilies deleteDataWithKey:saveUserVipKey];
        [TTLoginSingle deleteUserInfo];
        [[TTLoginSingle sharedSingle].loginSubject sendNext:@(0)];
        [self presentLoginViewController];
        
        LLSystemAlertView *alertView = [LLSystemAlertView alertViewWithTitle:@"登录异常" message:@"您的账号已经锁定，无法正常使用。" confirmTitle:@"确认" confirmBlock:^BOOL{
            return YES;
        }];
        [TYShowAlertView showAlertViewWithView:alertView backgoundTapDismissEnable:YES];
        
    }
}

// 金价行情
- (COPromise *)getMarketData
{
    COPromise *promise = [COPromise promise];
    
    [[LLNetworkManagerTool sharedManager] postUrlString:goldPrice_api parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [self.allGoldArray removeAllObjects];
        
        for (NSDictionary *dic in responseObject[@"PriceList"]) {
            TTMarketModel *model = [TTMarketModel modelWithJSON:dic];
            [self.allGoldArray addObject:model];
        }
        
        NSMutableArray *array = [self handleGoldData];
        
        [promise fulfill:co_tuple(responseObject[@"Description"], array)];
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        
        [promise fulfill:co_tuple(nil, nil, nil)];
        
    } showLoadingInView:nil];
    
    return promise;
}

// 处理金价数据
- (NSMutableArray *)handleGoldData
{
    NSDictionary *marketDic;
    TTLoginSingle *single = [TTLoginSingle sharedSingle];
    if (single.isLogin) {
        marketDic = [TTLoginSingle sharedSingle].marketDic;
    } else {
        marketDic = [TTLoginSingle sharedSingle].unLoginMarketDic;
    }
    NSMutableArray *array = [NSMutableArray array];
    for (TTMarketModel *model in self.allGoldArray) {
        model.isOpen = [marketDic[model.Code] boolValue];
        if (model.isOpen) {
            [array addObject:model];
        }
    }
    if (array.count == 0 && self.allGoldArray.count > 1) {
        //TODO: 未登录的话还未打开开关默认展示前两条，如果登录过了，但是用户没有自己改变开关状态也默认展示前两条
        if (!single.isLogin || ![USER_DEFAULT boolForKey:@"user_behavior"]) {
            TTMarketModel *model1 = self.allGoldArray.firstObject;
            model1.isOpen = YES;
            TTMarketModel *model2 = self.allGoldArray[1];
            model2.isOpen = YES;
            [array addObject:model1];
            [array addObject:model2];
            if (!single.isLogin) {
                [single.unLoginMarketDic setObject:@(YES) forKey:model1.Code];
                [single.unLoginMarketDic setObject:@(YES) forKey:model2.Code];
            } else {
                [single.marketDic setObject:@(YES) forKey:model1.Code];
                [single.marketDic setObject:@(YES) forKey:model2.Code];
            }
        }
    }
    
    [TTLoginSingle saveGoldStatus];

    return array;
}

// 设置标题字体
- (NSAttributedString *)typeAttributedText:(NSString *)type isSelected:(BOOL)isSelected {
    NSMutableAttributedString *attrubtedText = [[NSMutableAttributedString alloc] initWithString:type];
    NSRange range = [type rangeOfString:@"\n"];
    NSRange range1 = NSMakeRange(0, range.location);
    NSRange range2 = NSMakeRange(range.location + range.length, type.length - (range.location + range.length));
    [attrubtedText addAttributes:@{NSForegroundColorAttributeName : UIColorHex(333333), NSFontAttributeName : [UIFont boldSystemFontOfSize:18]} range:range1];
    [attrubtedText addAttributes:@{NSForegroundColorAttributeName : UIColorHex(999999), NSFontAttributeName : [UIFont systemFontOfSize:11]} range:range2];
    
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineSpacing = 5;  // 行间距
    style.alignment = NSTextAlignmentCenter;    // 居中
    [attrubtedText addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, type.length)];
    
    
    if (isSelected) {
        [attrubtedText addAttributes:@{NSForegroundColorAttributeName : kMainColor} range:range1];
        [attrubtedText addAttributes:@{NSForegroundColorAttributeName : UIColorHex(FFFFFF)} range:range2];
    }else {
        [attrubtedText addAttributes:@{NSForegroundColorAttributeName : UIColorHex(333333)} range:range1];
        [attrubtedText addAttributes:@{NSForegroundColorAttributeName : UIColorHex(999999)} range:range2];
    }
    return attrubtedText;
}


- (void)addTopSearchView
{
    UIView *searchBGView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, StatusBarAndNavBar_Height)];
    searchBGView.backgroundColor = UIColorHex(8553DF);
    [self.view addSubview:searchBGView];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, kStatusBarHeight, 54 * kScale, kNavBarHeight)];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.image = [UIImage imageNamed:@"淘托"];
    [searchBGView addSubview:imageView];
    
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    searchBtn.backgroundColor = UIColorHex(E6D8FF);
    searchBtn.frame = CGRectMake(imageView.right + 20, imageView.top + 7, SCREEN_WIDTH - imageView.right - 20 - 20, 30);
    searchBtn.layer.cornerRadius = 15;
    [searchBtn setImage:[UIImage imageNamed:@"home_search"] forState:UIControlStateNormal];
    [searchBtn setTitle:@"输入 成品/系列/品牌 搜索" forState:UIControlStateNormal];
    searchBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [searchBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [searchBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
    [searchBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 15, 0, 0)];
    [searchBtn addTarget:self action:@selector(searchBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [searchBGView addSubview:searchBtn];
    
    
}


// 置顶
- (void)toTopBtnClick
{
//    TTHomeListView *listView = (TTHomeListView *)self.listViews[self.currentIndex];
//    [listView.collectionView scrollToTopAnimated:YES];
}

- (void)gotoLoginOrRegist
{
    // 强制登录
    if ([TTLoginSingle sharedSingle].isLogin) {
        return;
    }
    // 是不是第一次使用
    BOOL isNotFirst = [USER_DEFAULT boolForKey:@"isNotFirstUse"];
    if (!isNotFirst) {
        // 第一次用,去注册页面
        TTRegisterViewController *registerVC = [[TTRegisterViewController alloc] initWithHiddenBackBarButton];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:registerVC];
        nav.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:nav animated:YES completion:nil];
        [USER_DEFAULT setBool:YES forKey:@"isNotFirstUse"];
    } else {
        [self presentLoginViewController];
    }
}

// 更新提示
- (void)updateAppHint
{
    @weakify(self)
    [[LLNetworkManagerTool sharedManager] requestMethod:GET urlString:updateApp_api parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        @strongify(self)
        
        // 获取手机当前版本号 如：1.05 * 1000，不要使用1.0.5这样的，计算会出错
        CGFloat currentVersionCode = [kCurrentVersion floatValue] * 1000;
        TTUpdateModel *updateModel = [TTUpdateModel modelWithJSON:responseObject];
        if (currentVersionCode <= updateModel.LoginVersionCode) {
            // 店铺可以显示
            [TTLoginSingle sharedSingle].mineDianpuShow = YES;
            // 强制登录
            [self gotoLoginOrRegist];
        }
        if (currentVersionCode <= updateModel.ForceVersionCode) {
            // 强制更新
            [self forceUpdateAppWithModel:updateModel];
        } else if (currentVersionCode < updateModel.VersionCode) {
            
            if (updateModel.IsForceUpdate) {
                // 要强制更新
                [self forceUpdateAppWithModel:updateModel];
            } else {
                // 非强制更新
                [self normalUpdateAppWithModel:updateModel];
            }
        }
        
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        
    } showLoadingInView:nil];
}

- (void)forceUpdateAppWithModel:(TTUpdateModel *)updateModel
{
    LLAlertView *alertView = [LLAlertView alertViewWithTopImageName:@"xinbanben" message:updateModel.UpdateContent confirmTitle:@"立即更新" confirmBlock:^BOOL{
        NSString *urlStr = [NSString stringWithFormat:@"https://itunes.apple.com/cn/app/id%@", APPID];
        NSURL *url = [NSURL URLWithString:urlStr];
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
            [[UIApplication sharedApplication] openURL:url];
        }
        return NO;
    }];
    [TYShowAlertView showAlertViewWithView:alertView backgoundTapDismissEnable:NO];
}

- (void)normalUpdateAppWithModel:(TTUpdateModel *)updateModel
{
    LLAlertView *alertView = [LLAlertView alertViewWithTopImageName:@"xinbanben" message:updateModel.UpdateContent cancelTitle:@"稍后再说" cancelBlock:nil confirmTitle:@"立即更新" confirmBlock:^BOOL{
        NSString *urlStr = [NSString stringWithFormat:@"https://itunes.apple.com/cn/app/id%@", APPID];
        NSURL *url = [NSURL URLWithString:urlStr];
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
            [[UIApplication sharedApplication] openURL:url];
        }
        return NO;
    }];
    [TYShowAlertView showAlertViewWithView:alertView backgoundTapDismissEnable:NO];
}

///用户上线了
- (void)userOnlineNotify
{
    if (![TTLoginSingle sharedSingle].isLogin) {
        return;
    }
    NSDictionary *dic = @{
        @"SendType" : @2,
        @"AccountId" : [TTLoginSingle sharedSingle].loginModel.AccountId,
        @"RecordType" : @1,
        @"Type" : @1,
        @"RecordContent" : @"客户上线通知-ios",
        @"Attr" : @"",
        @"WlChatInfoId" : [TTLoginSingle sharedSingle].loginModel.ChatInfoId,
        @"CustomerId" : [TTLoginSingle sharedSingle].loginModel.ChatMark,
        @"AccountName" : [TTLoginSingle sharedSingle].loginModel.Name,
        @"DeviceNumber" : [LLPasswordManagerUtilies getUUIDString],
        @"TranslateId" : [TTLoginSingle sharedSingle].loginModel.TranslateId,
        @"StrAccountId" : @""
    };
    [[LLNetworkManagerTool sharedManager] postJsonWithUrlString:userOnlineNotify_api parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        
    } showLoadingInView:nil];
}


- (TTNewHomeListView *)listView
{
    if (!_listView) {
        _listView = [[TTNewHomeListView alloc] initWithFrame:CGRectMake(0, StatusBarAndNavBar_Height, SCREEN_WIDTH, SCREEN_HEIGHT - StatusBarAndNavBar_Height - Tabbar_Height)];
        [self.view addSubview:_listView];
    }
    return _listView;
}

- (NSMutableArray *)allGoldArray
{
    if (!_allGoldArray) {
        _allGoldArray = [NSMutableArray array];
    }
    return _allGoldArray;
}


- (UIButton *)toTopBtn
{
    if (!_toTopBtn) {
        _toTopBtn = [UIButton createButtonWithImage:[UIImage imageNamed:@"zhiding"] target:self action:@selector(toTopBtnClick)];
        _toTopBtn.frame = CGRectMake(self.view.width - 50, self.view.height - 50, 40, 40);
        _toTopBtn.hidden = YES;
        _toTopBtn.backgroundColor = [UIColor whiteColor];
        _toTopBtn.layer.cornerRadius = 20;
        _toTopBtn.layer.shadowColor = [UIColor blackColor].CGColor;
        _toTopBtn.layer.shadowOffset = CGSizeZero;
        _toTopBtn.layer.shadowOpacity = 0.3;
        [self.view addSubview:_toTopBtn];
    }
    return _toTopBtn;
}

- (TTHongBaoView *)hongbaoView
{
    if (!_hongbaoView) {
        _hongbaoView = [TTHongBaoView createViewFromNib];
    }
    return _hongbaoView;
}

@end
